home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 January / enter-2004-01.iso / files / maxima-5.9.0.exe / {app} / share / maxima / 5.9.0 / src / rpart.lisp < prev    next >
Encoding:
Text File  |  2003-02-09  |  18.4 KB  |  519 lines

  1. ;;; -*-  Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
  2. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  3. ;;;     The data in this file contains enhancments.                    ;;;;;
  4. ;;;                                                                    ;;;;;
  5. ;;;  Copyright (c) 1984,1987 by William Schelter,University of Texas   ;;;;;
  6. ;;;     All rights reserved                                            ;;;;;
  7. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  8. ;;;     (c) Copyright 1982 Massachusetts Institute of Technology         ;;;
  9. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  10.  
  11. (in-package "MAXIMA")
  12. (macsyma-module rpart)
  13.  
  14. ;;;    Complex variable utilities
  15. ;;;
  16. ;;; Macsyma functions: $realpart $imagpart $rectform $polarform
  17. ;;;               $cabs $carg
  18. ;;; Utility functions: trisplit risplit absarg cabs andmapc andmapcar
  19.  
  20. (load-macsyma-macros rzmac)
  21.  
  22. (declare-top (special negp* $%emode $radexpand rp-polylogp
  23.           $domain $m1pbranch $logarc rischp $keepfloat
  24.           complexsign)
  25.      (*lexpr $expand)
  26.      (genprefix ~rp))
  27.  
  28. (defmvar implicit-real nil "If t RPART assumes radicals and logs
  29.          of real quantities are real and doesn't ask sign questions")
  30.  
  31. (defmvar generate-atan2 t "Controls whether RPART will generate ATAN's
  32.                     or ATAN2's, default is to make ATAN2's")
  33.  
  34. (defmfun $realpart (xx) (car (trisplit xx)))
  35.  
  36. (defmfun $imagpart (xx) (cdr (trisplit xx)))
  37.  
  38. ;;;Rectform gives a result of the form a+b*%i.
  39.  
  40. (defmfun $rectform (xx)
  41.   (let ((ris (trisplit xx)))
  42.        (add (car ris) (mul (cdr ris) '$%i))))
  43.  
  44. ;;;Polarform gives a result of the form a*%e^(%i*b).
  45.  
  46. (defmfun $polarform (xx)
  47.        (cond ((and (not (atom xx)) (memq (caar xx) '(mequal mlist $matrix)))
  48.           (cons (car xx) (mapcar #'$polarform (cdr xx))))
  49.          (t ((lambda (aas $%emode)
  50.           (mul (car aas) (powers '$%e (mul '$%i (cdr aas)))))
  51.          (absarg xx) nil))))
  52.  
  53. ;;; Cabs gives the complex absolute value.  Nota bene: an expression may
  54. ;;; be syntactically real without being real (e.g. sqrt(x), x<0).  Thus
  55. ;;; Cabs must lead an independent existence from Abs.
  56.  
  57. (defmfun $cabs (xx) (cabs ($rectform xx)))
  58.  
  59. ;;; Carg gives the complex argument.
  60. (defmfun $carg (xx) (cdr (absarg xx)))
  61.  
  62. (defvar absflag nil)
  63.  
  64. ;; The function of Absflag is to communicate to Absarg that only the absolute
  65. ;; value part of the result is wanted.  This allows Absarg to avoid asking
  66. ;; questions irrelevant to the absolute value.  For instance, Cabs(x) is
  67. ;; invariably Abs(x), while the complex phase may be 0 or %pi.  Note also
  68. ;; the steps taken in Absarg to assure that Asksign's will happen before Sign's
  69. ;; as often as possible, so that, for instance, Abs(x) can be simplified to
  70. ;; x or -x if the sign of x must be known for some other reason.  These
  71. ;; techniques, however, are not perfect.
  72.  
  73. ;; The internal cabs, used by other Macsyma programs.
  74. (defmfun cabs (xx) (let ((absflag t)) (car (absarg xx))))
  75.  
  76. ;; Some objects can only appear at the top level of a legal simplified 
  77. ;; expression: CRE forms and equations in particular.
  78.  
  79. (defmfun trisplit (el)    ;Top level of risplit
  80.   (cond ((atom el) (risplit el))
  81.     ((specrepp el) (trisplit (specdisrep el)))
  82.     ((eq (caar el) 'mequal) (dot-sp-ri (cdr el) '(mequal simp)))
  83.     (t (risplit el)))) 
  84.  
  85. ;;; Auxiliaries
  86.  
  87. ;; These are Macsyma equivalents to (mapcar 'trisplit ...).  They must
  88. ;; differ from other maps for two reasons: the lists are Macsyma lists,
  89. ;; and therefore prefixed with list indicators; and the results must
  90. ;; be separated: ((a . b) (c . d)) becomes something like ([a,c].[b,d]).
  91.  
  92. (defun dsrl (el) (dot-sp-ri (cdr el) '(mlist simp)))
  93.  
  94. (defun dot-sp-ri (el ind)
  95.     (dot--ri (mapcar #'trisplit el) ind))
  96.  
  97. ;; Dot--ri does the ((a.b)(c.d))->([a,c].[b,d]) transformation with
  98. ;; minimal Cons'ing.
  99.  
  100. (defun dot--ri (el ind) 
  101.        (do ((i el (cdr i)) (k))
  102.        ((null i) (cons (cons ind (nreverse k)) (cons ind el)))
  103.        ((lambda (cdari) (setq k (rplacd (car i) k))
  104.                 (rplaca i cdari))
  105.         (cdar i))))
  106.  
  107. (defun risplit-mplus (l)
  108.     (do ((rpart) (ipart) (m (cdr l) (cdr m)))
  109.     ((null m) (cons (addn rpart t) (addn ipart t)))
  110.     ((lambda (sp) 
  111.          (cond ((=0 (car sp)))
  112.                (t (setq rpart (cons (car sp) rpart))))
  113.          (cond ((=0 (cdr sp)))
  114.                (t (setq ipart (cons (cdr sp) ipart)))))
  115.      (risplit (car m)))))
  116.  
  117. (defun risplit-times (l)
  118.   ((lambda (risl)
  119.       (cond ((null (cdr risl)) (cons (muln (car risl) t) 0))
  120.         (t (do ((rpart 1) (ipart 0) (m (cdr risl) (cdr m))) 
  121.            ((null m)
  122.             (cons (muln (cons rpart (car risl)) t)
  123.               (muln (cons ipart (car risl)) t)))
  124.            (psetq rpart (sub (mul rpart (caar m))
  125.                      (mul ipart (cdar m)))
  126.               ipart (add (mul ipart (caar m))
  127.                      (mul rpart (cdar m))))))))
  128.    (do ((purerl nil) (compl nil) (l (cdr l) (cdr l)))
  129.        ((null l) (cons purerl compl))
  130.        ;;This is what Risl is bound to
  131.        ((lambda (sp)
  132.        (cond ((=0 (cdr sp)) (setq purerl (rplacd sp purerl)))
  133.          ((or (atom (car sp)) (atom (cdr sp)))
  134.           (setq compl (cons sp compl)))
  135.          ((and (eq (caaar sp) 'mtimes)
  136. ;;;Try risplit z/w and notice denominator.  If this check were not made,
  137. ;;; the real and imaginary parts would not each be over a common denominator.
  138.                (eq (caadr sp) 'mtimes)
  139.                ((lambda (nr ni)
  140.               (cond ((equal (car nr) (car ni))
  141.                  (setq
  142.                     purerl (cons (car nr) purerl)
  143.                     compl
  144.                      (cons (cons (muln (nreverse (cdr nr)) t)
  145.                          (muln (nreverse (cdr ni)) t))
  146.                        compl)))
  147.                 (t (nreverse nr) (nreverse ni) nil)))
  148.             (nreverse (cdar sp))
  149.             (nreverse (cddr sp)))))
  150.          (t (setq compl (cons sp compl)))))
  151.     (risplit (car l))))))
  152.  
  153. (defun risplit-expt (l)
  154.   ((lambda (pow $radexpand ris)  ; Don't want 'simplifications' like
  155.      (cond              ; Sqrt(-x) -> %i*sqrt(x)
  156.       ((fixnump pow) 
  157.        ((lambda (sp)
  158.       (cond ((= pow -1)
  159.          ((lambda (a2+b2)
  160.              (cons (div (car sp) a2+b2)
  161.                (mul -1 (div (cdr sp) a2+b2))))
  162.           (spabs sp)))
  163.         ((> (abs pow) $maxposex)
  164.          (cond ((=0 (cdr sp)) (cons (powers (car sp) pow) 0))
  165.                (t ((lambda (abs^n natan)
  166.                  (cons (mul abs^n
  167.                     (take '(%cos) natan))
  168.                    (mul abs^n (take '(%sin) natan))))
  169.                (powers (add (powers (car sp) 2)
  170.                     (powers (cdr sp) 2))
  171.                    (*red pow 2))
  172.                (mul pow (genatan (cdr sp) (car sp)))))))
  173.         ((> pow 0) (expanintexpt sp pow))
  174.         (t ((lambda (abbas basspli)
  175.                 (cons (div (car basspli) abbas)
  176.                   (neg (div (cdr basspli) abbas))))
  177.             (powers (spabs sp) (f- pow))
  178.             (expanintexpt sp (f- pow))))))
  179.     (risplit (cadr l))))
  180.       ((and (ratnump pow)
  181.         (fixnump (cadr pow))
  182.         (not (< (cadr pow) (f- $maxnegex)))
  183.         (not (> (cadr pow) $maxposex))
  184.         (prog2 (setq ris (risplit (cadr l)))
  185.            (or (= (caddr pow) 2) (=0 (cdr ris)))))
  186.        (cond ((=0 (cdr ris))
  187.           (case (cond ((mnegp (car ris)) '$negative)
  188.                (implicit-real '$positive)
  189.                (t (asksign (car ris))))
  190.         ($negative (risplit (mul2 (power -1 pow) (power (neg (car ris)) pow))))
  191.         ($zero (cons (power 0 pow) 0))
  192.         (t (cons (power (car ris) pow) 0))))
  193.          (t ((lambda (abs2 n pos?)
  194.           ((lambda (abs)
  195.             (divcarcdr
  196.              (expanintexpt 
  197.               (cons (power (add abs (car ris)) (1//2))
  198.                 (porm ((lambda (a b) (cond (a (not b)) (b t))) ;Xor
  199.                    pos? (eq (asksign (cdr ris)) '$negative))
  200.                   (power (sub abs (car ris)) (1//2))))
  201.               n)
  202.              (cond (pos? (power 2 (div n 2)))
  203.                (t (power (mul 2 abs2) (div n 2))))))
  204.            (power abs2 (1//2))))
  205.          (spabs ris) (abs (cadr pow)) (> (cadr pow) -1)))))
  206.       ((and (floatp (setq ris (cadr l))) (floatp pow))
  207.        (risplit ((lambda ($numer) (exptrl ris pow)) t)))
  208.       (t ((lambda (sp aa)
  209.        ;;If all else fails, we use the trigonometric form.
  210.        (cond ((and (=0 (cdr sp)) (=0 (cdr aa))) (cons l 0))
  211.          (t ((lambda (pre post)
  212.               (cons (mul pre (take '(%cos) post))
  213.                 (mul pre (take '(%sin) post))))
  214.              (mul (powers '$%e (mul (cdr aa) (mul (cdr sp) -1)))
  215.               (powers (car aa) (car sp)))
  216.              (add (mul (cdr sp) (take '(%log) (car aa)))
  217.               (mul (car sp) (cdr aa)))))))
  218.       (risplit (caddr l)) (absarg1 (cadr l)))))) 
  219.    (caddr l) nil nil))
  220.  
  221. (defun risplit-noun (l)
  222.   (cons (simplify (list '(%realpart) l)) (simplify (list '(%imagpart) l))))
  223.  
  224. (defun absarg1 (arg)
  225.   (let ((arg1 arg) ($keepfloat t))
  226.     (cond ((and (or (free arg '$%i)
  227.             (free (setq arg1 (sratsimp arg)) '$%i))
  228.         (not (eq (csign arg1) t)))
  229.        (setq arg arg1)
  230.        (if implicit-real
  231.            (cons arg 0)
  232.            (unwind-protect
  233.          (prog2 (assume `(($notequal) ,arg 0)) 
  234.             (absarg arg))
  235.          (forget `(($notequal) ,arg 0)))))
  236.       (t (absarg arg)))))
  237.  
  238. ;;;    Main function
  239. ;;; Takes an expression and returns the dotted pair
  240. ;;; (<Real part> . <imaginary part>).
  241.  
  242. (defun risplit (l) 
  243.   (let (($domain '$complex) ($m1pbranch t) $logarc op)
  244.     (cond
  245.      ((atom l) (cond ((eq l '$%i) (cons 0 1))
  246.              ((decl-complexp l) (risplit-noun l))
  247.              (t (cons l 0))))
  248.      ((eq (caar l) 'rat) (cons l 0))
  249.      ((eq (caar l) 'mplus) (risplit-mplus l))
  250.      ((eq (caar l) 'mtimes) (risplit-times l))
  251.      ((eq (caar l) 'mexpt) (risplit-expt l))
  252.      ((eq (caar l) '%log)
  253.       (let ((aa (absarg1 (cadr l))))
  254.        (rplaca aa (take '(%log) (car aa)))))
  255.      ((eq (caar l) 'bigfloat) (cons l 0))        ;All numbers are real.
  256.      ((and (memq (caar l) '(%integrate %derivative %laplace %sum))
  257.        (freel (cddr l) '$%i))
  258.       (let ((ris (risplit (cadr l))))
  259.        (cons (simplify (list* (ncons (caar l)) (car ris) (cddr l)))
  260.              (simplify (list* (ncons (caar l)) (cdr ris) (cddr l))))))
  261.      (((lambda (ass)
  262. ;;;This clause handles the very similar trigonometric and hyperbolic functions.
  263. ;;; It is driven by the table at the end of the lambda.
  264.      (and ass
  265.           ((lambda (ri)
  266.                (cond ((=0 (cdr ri))        ;Pure real case.
  267.                   (cons (take (list (car ass)) (car ri)) 0))
  268.                  (t (cons (mul (take (list (car ass)) (car ri))
  269.                        (take (list (cadr ass)) (cdr ri)))
  270.                       (negate-if (eq (caar l) '%cos)
  271.                          (mul (take (list (caddr ass))
  272.                                 (car ri))
  273.                               (take (list (cdddr ass))
  274.                                 (cdr ri))))))))
  275.            (risplit (cadr l)))))
  276.        (assq (caar l)
  277.          '((%sin %cosh %cos . %sinh)
  278.            (%cos %cosh %sin . %sinh)
  279.            (%sinh %cos %cosh . %sin)
  280.            (%cosh %cos %sinh . %sin)))))
  281.      ((memq (caar l) '(%tan %tanh))
  282.       ((lambda (sp)
  283. ;;;The similar tan and tanh cases.
  284.            (cond ((=0 (cdr sp)) (cons l 0))
  285.              (t 
  286.               ((lambda (2rl 2im)
  287.                    ((lambda (denom)
  288.                     (cond ((eq (caar l) '%tan)
  289.                            (cons (mul (take '(%sin) 2rl) denom)
  290.                              (mul (take '(%sinh) 2im) denom)))
  291.                           (t (cons (mul (take '(%sinh) 2rl) denom)
  292.                                (mul (take '(%sin) 2im) denom)))))
  293.                 (inv (cond ((eq (caar l) '%tan)
  294.                         (add (take '(%cosh) 2im) (take '(%cos) 2rl)))
  295.                        (t (add (take '(%cos) 2im) (take '(%cosh) 2rl)))))))
  296.                (mul (car sp) 2)
  297.                (mul (cdr sp) 2)) )))
  298.        (risplit (cadr l))))
  299.      ((and (memq (caar l) '(%atan %csc %sec %cot %csch %sech %coth))
  300.        (=0 (cdr (risplit (cadr l)))))
  301.       (cons l 0))
  302.      ((and (eq (caar l) '$atan2) (=0 (cdr (risplit (div (cadr l) (caddr l))))))
  303.       (cons l 0))
  304.      ((or (arcp (caar l)) (eq (caar l) '$atan2))
  305.       (let ((ans (risplit ((lambda ($logarc) (ssimplifya l)) t))))
  306.        (cond ((eq (caar l) '$atan2)
  307.           (setq ans (cons (sratsimp (car ans)) (sratsimp (cdr ans))))))
  308.        (cond ((and (free l '$%i) (=0 (cdr ans))) (cons l 0)) (t ans))))
  309.      ((eq (caar l) '%plog)
  310.                             ;  (princ '|Warning: Principal value not guaranteed for Plog in Rectform/
  311.                             ;|)
  312.       (risplit (cons '(%log) (cdr l))))
  313.      ((memq (caar l) '(%realpart %imagpart mabs)) (cons l 0))
  314.      ((eq (caar l) '%erf)
  315.       (let ((ris (risplit (cadr l))) orig cc)
  316.        (setq orig (simplify (list '(%erf) (add (car ris) (mul '$%i (cdr ris))))))
  317.        (setq cc (simplify (list '(%erf) (sub (car ris) (mul '$%i (cdr ris))))))
  318.        (cons (div (add orig cc) 2) (div (sub orig cc) (mul 2 '$%i)))))
  319. ;;; ^ All the above are guaranteed pure real.
  320. ;;; The handling of lists and matrices below has to be thought through.
  321.      ((eq (caar l) 'mlist) (dsrl l))
  322.      ((eq (caar l) '$matrix)
  323.       (dot--ri (mapcar #'dsrl (cdr l)) '($matrix simp)))
  324.      ((memq (caar l) '(mlessp mleqp mgreaterp mgeqp))
  325.       (let ((ris1 (risplit (cadr l))) (ris2 (risplit (caddr l))))
  326.        (cons (simplify (list (ncons (caar l)) (car ris1) (car ris2)))
  327.              (simplify (list (ncons (caar l)) (cdr ris1) (cdr ris2))))))
  328. ;;;The Coversinemyfoot clause covers functions which can be converted
  329. ;;; to functions known by risplit, such as the more useless trigonometrics.
  330.      (((lambda (foot) (and foot (risplit foot)))
  331.        (coversinemyfoot l)))
  332. ;;; A MAJOR ASSUMPTION:
  333. ;;;  All random functions are pure real, regardless of argument.
  334. ;;;  This is evidently assumed by some of the integration functions.
  335. ;;;  Perhaps the best compromise is to return 'realpart/'imagpart
  336. ;;;   under the control of a switch set by the integrators.  First
  337. ;;;   all such dependencies must be found in the integ
  338.      ((and rp-polylogp (mqapplyp l) (eq (subfunname l) '$li)) (cons l 0))
  339.      ((prog2 (setq op (if (eq (caar l) 'mqapply) (caaadr l) (caar l)))
  340.          (decl-complexp op))
  341.       (risplit-noun l))
  342.      ((and (eq (caar l) '%product) (not (free (cadr l) '$%i)))
  343.       (risplit-noun l))
  344.      (t (cons l 0)))))
  345.  
  346. (defun coversinemyfoot (l)
  347.   (prog (recip)
  348.     (cond ((not (memq (caar l) '(%csc %sec %cot %csch %sech %coth))))
  349.           ((null (setq recip (get (caar l) 'recip))))
  350.           (t (return (div 1 (cons (list recip) (cdr l))))))))
  351.  
  352. (defun powers (c d) 
  353.        (cond ((=1 d) c)
  354.          ((equal d 0) 1)          ;equal to preclude 0^(pdl 0)->0:
  355.          ((=0 c) 0)              ; see comment before =0.
  356.          ((=1 c) 1)
  357.          (t (power c d))))
  358.  
  359. (defun spabs (sp) (add (powers (car sp) 2) (powers (cdr sp) 2)))
  360.  
  361. (progn (setq negp* '(nil nil t t))
  362.        (nconc negp* negp*)
  363.        0)
  364.  
  365. (defun divcarcdr (a b) (cons (div (car a) b) (div (cdr a) b)))
  366.  
  367. (declare-top (notype (expanintexpt notype fixnum)))
  368.  
  369. ;Expand bas^n, where bas is (<real part> . <imaginary part>)
  370.  
  371. (defun expanintexpt (bas n)
  372.   (cond ((= n 1) bas)
  373.         (t (do ((rp (car bas))
  374.         (ip (cdr bas))
  375.         (c 1 (quotient (times c ex) i))
  376.         (ex n (f1- ex)) (i 1 (f1+ i))
  377.         (rori t (not rori)) (negp negp* (cdr negp))
  378.         (rpt nil) (ipt nil))
  379.            ((< ex 0) (cons (addn rpt t) (addn ipt t)))
  380.            (declare (fixnum ex i))
  381.            (set-either rpt ipt
  382.                rori
  383.                (cons (negate-if (car negp)
  384.                         (mul c
  385.                          (powers rp ex)
  386.                          (powers ip (f1- i))))
  387.                  (cond (rori rpt) (t ipt))))))))
  388.  
  389.  
  390.  
  391. ;;;   Subtract out multiples of 2*%pi with a minimum of consing.
  392. ;;;   Attempts to reduce to interval (-pi,pi].
  393.  
  394. (defun 2pistrip (exp)
  395.   (cond ((atom exp) exp)
  396.     ((eq (caar exp) 'mtimes)
  397.      (cond ((and (mnump (cadr exp))
  398.              (eq (caddr exp) '$%pi)
  399.              (null (cdddr exp)))
  400.         (cond ((integerp (cadr exp))        ;5*%pi
  401.                (mul (abs (remainder (cadr exp) 2)) '$%pi))
  402.                        ;Neither 0 nor 1 appears as a coef
  403.               ((eq 'rat (caaadr exp))        ;5/2*%pi
  404.                (mul (list* '(rat simp)
  405.                  (sub1 (remainder (add1 (cadadr exp))
  406.                           (times 2 (caddadr exp))))
  407.                  (cddadr exp))
  408.                 '$%pi))
  409.               (t exp)))
  410.            (t exp)))
  411.     ((eq (caar exp) 'mplus)
  412.      ((lambda (res)
  413.         (cond ((eq res (cdr exp)) exp) (t (addn res t))))
  414.       (2pirec (cdr exp))))
  415.     (t exp)))
  416.  
  417. (defun 2pirec (fm)                ;Takes a list of exprs
  418.     (cond ((null (cdr fm))            ;If monad, just return.
  419.            ((lambda (2pf)
  420.           (cond ((eq 2pf (car fm)) fm)
  421.             ((=0 2pf) nil)
  422.             (t (list 2pf))))
  423.         (2pistrip (car fm))))
  424.           (t ((lambda (2pfma 2pfmd)
  425.              (cond ((or (null 2pfmd) (=0 2pfmd)) 2pfma)
  426.                ((and (eq 2pfmd (cdr fm)) (eq 2pfma (car fm))) fm)
  427.                (t (cons 2pfma 2pfmd))))
  428.           (2pistrip (car fm)) (2pirec (cdr fm))))))
  429.  
  430. ;;;    Rectify into polar form; Arguments similar to risplit
  431.  
  432. (defun argnum (n) (cond ((minusp n) (simplify '$%pi)) (t 0)))
  433.  
  434. (defun absarg (l)
  435.        (setq l ($expand l))
  436.        (cond ((atom l)
  437.           (cond ((eq l '$%i)
  438.              (cons 1 (simplify '((mtimes) ((rat simp) 1 2) $%pi))))
  439.             ((numberp l)
  440.              (cons (abs l) (argnum l)))
  441.             ((memq l '($%e $%pi)) (cons l 0))
  442.             (absflag (cons (take '(mabs) l) 0))
  443.             (t ((lambda (gs)
  444.                 (cond ((eq gs '$positive) (cons l 0))
  445.                       ((eq gs '$zero) (cons 0 0))
  446.                       ((eq gs '$negative)
  447.                        (cons (neg l) (simplify '$%pi)))
  448.                       (t (cons (take '(mabs) l) 0))))
  449.             (cond ((eq rischp l) '$positive) (t (asksign l)))))))
  450.          ((memq (caar l) '(rat bigfloat))
  451.           (cons (list (car l) (abs (cadr l)) (caddr l))
  452.             (argnum (cadr l))))
  453.          ((eq (caar l) 'mtimes)
  454.           (do ((n (cdr l) (cdr n))
  455.            (abars)
  456.            (argl () (cons (cdr abars) argl))
  457.            (absl () (rplacd abars absl)))
  458.           (())
  459.           (cond ((not n)
  460.              (return (cons (muln absl t)
  461.                        (2pistrip (addn argl t))))))
  462.           (setq abars (absarg (car n)))))
  463.          ((eq (caar l) 'mexpt)
  464.           (let ((aa (absarg (cadr l)))
  465.             (sp (risplit (caddr l)))
  466.             ($radexpand nil))
  467.            (cons (mul (powers (car aa) (car sp))
  468.                   (powers '$%e (neg (mul (cdr aa) (cdr sp)))))
  469.              (add (mul (cdr aa) (car sp))
  470.                   (mul (cdr sp) (take '(%log) (car aa)))))))
  471.          ((and (memq (caar l) '(%tan %tanh))
  472.            (not (=0 (cdr (risplit (cadr l))))))
  473.           ((lambda (sp)
  474.                ((lambda (2frst 2scnd)
  475.                 (cond ((eq (caar l) '%tanh)
  476.                        (psetq 2frst 2scnd 2scnd 2frst)))
  477.                 (cons ((lambda (cosh cos)
  478.                            (root (div (add cosh (neg cos))
  479.                               (add cosh cos))
  480.                              2))
  481.                        (take '(%cosh) 2frst)
  482.                        (take '(%cos) 2scnd))
  483.                       (take '(%atan)
  484.                         (cond ((eq (caar l) '%tan)
  485.                            (div (take '(%sinh) 2frst)
  486.                             (take '(%sin) 2scnd)))
  487.                           (t (div (take '(%sin) 2frst)
  488.                               (take '(%sinh) 2scnd)))))))
  489.             (mul (cdr sp) 2)
  490.             (mul (car sp) 2)))
  491.            (risplit (cadr l))))
  492.          ((specrepp l) (absarg (specdisrep l)))
  493.          (((lambda (foot)
  494.         (and foot (not (=0 (cdr (risplit (cadr l))))) (absarg foot)))
  495.            (coversinemyfoot l)))
  496.          (t (let ((ris (trisplit l)))
  497.              (xcons
  498. ;;; Arguments must be in this order so that the side-effect of the Atan2,
  499. ;;; that is, determining the Asksign of the argument, can happen before
  500. ;;; Take Mabs does its Sign.  Blame JPG for noticing this lossage.
  501.                 (if absflag 0 (genatan (cdr ris) (car ris)))
  502.                 (cond ((equal (car ris) 0) (absarg-mabs (cdr ris)))
  503.                   ((equal (cdr ris) 0) (absarg-mabs (car ris)))
  504.                   (t (powers ($expand (add (powers (car ris) 2)
  505.                                (powers (cdr ris) 2))
  506.                               1 0)
  507.                          (half)))))))))
  508.  
  509. (defun genatan (num den)
  510.       (let ((arg (take '($atan2) num den)))
  511.        (if (or generate-atan2 (free arg '$atan2))
  512.            arg
  513.            (take '(%atan) (m// num den)))))
  514.  
  515. (defun absarg-mabs (l)
  516.   (if (eq (csign l) t)
  517.       (if (memq (caar l) '(mabs %cabs)) l (list '(%cabs simp) l))
  518.       (take '(mabs) l)))
  519.